package com.stu.day11.demoFactory;

/**
 * 简单工厂设计模式
 * @author zy
 * @date 2025/2/3 16:40
 */
// 产品接口
public interface ElectronicDevice {  // 电子设备
    void assemble();
}

// 手机类（具体产品A）
class Smartphone implements ElectronicDevice {
    private String networkConfig;
    private String osVersion;

    public Smartphone(String networkConfig, String osVersion) {
        this.networkConfig = networkConfig;
        this.osVersion = osVersion;
    }

    @Override
    public void assemble() {
        System.out.println("Assembling Smartphone with network: " + networkConfig + " and OS version: " + osVersion);
    }
}

// 电脑类（具体产品B）
class Computer implements ElectronicDevice {
    private String cpuModel;
    private String ramSize;

    public Computer(String cpuModel, String ramSize) {
        this.cpuModel = cpuModel;
        this.ramSize = ramSize;
    }

    @Override
    public void assemble() {
        System.out.println("Assembling Computer with CPU: " + cpuModel + " and RAM: " + ramSize);
    }
}

// 电视类（具体产品C）
class Television implements ElectronicDevice {
    private String resolution;
    private boolean isSmartTV;

    public Television(String resolution, boolean isSmartTV) {
        this.resolution = resolution;
        this.isSmartTV = isSmartTV;
    }

    @Override
    public void assemble() {
        System.out.println("Assembling Television with resolution: " + resolution + " and SmartTV: " + isSmartTV);
    }
}

// 工厂类：负责根据不同配置创建不同的设备
class ElectronicDeviceFactory {
    public static ElectronicDevice createDevice(String type, String config1, String config2) {
        // 根据传入的类型和配置参数决定创建哪种设备
        if ("Smartphone".equalsIgnoreCase(type)) {
            // 需要的网络配置和操作系统版本
            return new Smartphone(config1, config2);
        } else if ("Computer".equalsIgnoreCase(type)) {
            // 需要的CPU型号和RAM大小
            return new Computer(config1, config2);
        } else if ("Television".equalsIgnoreCase(type)) {
            // 需要的分辨率和是否为智能电视
            return new Television(config1, Boolean.parseBoolean(config2));
        } else {
            throw new IllegalArgumentException("Unknown device type: " + type);
        }
    }
}

// 客户端代码
class Client {
    public static void main(String[] args) {
        // 使用工厂创建不同类型的设备

        // 创建手机设备
        ElectronicDevice smartphone = ElectronicDeviceFactory.createDevice("Smartphone", "4G", "Android 12");
        smartphone.assemble();  // 输出：Assembling Smartphone with network: 4G and OS version: Android 12

        // 创建电脑设备
        ElectronicDevice computer = ElectronicDeviceFactory.createDevice("Computer", "Intel i9", "32GB");
        computer.assemble();  // 输出：Assembling Computer with CPU: Intel i9 and RAM: 32GB

        // 创建电视设备
        ElectronicDevice television = ElectronicDeviceFactory.createDevice("Television", "4K", "true");
        television.assemble();  // 输出：Assembling Television with resolution: 4K and SmartTV: true
    }
}
